<?php
// +----------------------------------------------------------------------
// | OneKeyAdmin [ Believe that you can do better ]
// +----------------------------------------------------------------------
// | Copyright (c) 2020-2023 http://onekeyadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: MUKE <513038996@qq.com>
// +----------------------------------------------------------------------
namespace plugins\cms\api\model;

use think\Model;
use app\index\addons\Url;
use app\api\model\Config;
use app\api\model\Catalog;

class Address extends Model
{
    protected $name = 'cms_address';

    // 设置json类型字段
    protected $json = ['field'];

    // 设置JSON数据返回数组
    protected $jsonAssoc = true;

    // 搜索器
    public function searchStatusAttr($query, $value, $array)
    {
        if ($value !== '' && $value !== null) {
            $query->where("status", '=', $value);
        }
    }

    public function searchKeywordAttr($query, $value, $array)
    {
        if (! empty($value)) {
            $query->where("title|address|nickname|phone", 'like', '%' . $value . '%');
        }
    }

    public function searchDateAttr($query, $value, $array)
    {
        if (! empty($value)) { 
            $query->whereBetweenTime('create_time', $value[0], $value[1]);
        }
    }
    
    public function searchAttrAttr($query, $value, $array)
    {
        if (! empty($value)) {
            $query->where($value, '=', 1);
        }
    }

    public function searchCatalogIdAttr($query, $value, $array)
    {
        if (! empty($value)) {
            $query->where('catalog_id', 'find in set', $value);
        }
    }

    // 获取器
    public function getCatalogTitleAttr($value, $array)
    {
        return $array['catalog_id'] ? Catalog::whereIn('id', $array['catalog_id'])->column('title') : [];
    }

    public function getUrlAttr($value, $array)
    {
        return Url::single($array);
    }

    public function getFieldAttr($value, $array)
    {
        $field = [];
        $config = Config::getVal('app_cms');
        if (isset($config['address_field'])) {
            foreach ($config['address_field'] as $k => $v) {
                switch ($v['type']['is']) {
                    case 'el-link-select':
                        $field[$v['field']] = isset($value[$v['field']]) ? Url::appoint($value[$v['field']]) : '';
                        break;
                    case 'el-array':
                        $arr = isset($value[$v['field']]) ? $value[$v['field']]['table'] : [];
                        foreach ($arr as $key1 => $val1) {
                            foreach (array_keys($val1) as $key2 => $val2) {
                                if (is_array($val1[$val2])) {
                                    $arr[$key1][$val2] = Url::appoint($val1[$val2]);
                                }
                            }
                        }
                        $field[$v['field']] = $arr;
                        break;
                    default:
                        $field[$v['field']] = isset($value[$v['field']]) ? $value[$v['field']] : '';
                        break;
                }
            }
        }
        return $field;
    }
}